package jp.sourceforge.akjrcp.text_douga.video;

import java.io.File;
import java.io.IOException;

import jp.sourceforge.akjrcp.text_douga.AudioData;
import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.VideoData;
import jp.sourceforge.akjrcp.text_douga.VideoWriter;
import jp.sourceforge.akjrcp.text_douga.effect.VideoEffect;

public class FileVideoData extends EffectableVideoData{
	private String inputPath;
	private double startTime;
	private double duration;
	private double loopPadTime;//loop only
	private int loop;
	private boolean canStopInLoop=true;//exactlly time base
	private AudioData audioData;
	private int padColor;
	
	
	
	private double logicalTime;

	public String toString(){
	return "start="+startTime+" duration="+duration+" loop="+loop+" canStopInLoop="+canStopInLoop+" inputPath="+inputPath;	
	}
	public FileVideoData(String inputPath){
		this.inputPath=inputPath;
	}
	public boolean isCanStopInLoop() {
		return canStopInLoop;
	}
	public void setCanStopInLoop(boolean canStopInLoop) {
		this.canStopInLoop = canStopInLoop;
	}
	public double getDuration() {
		return duration;
	}
	public void setDuration(double duration) {
		this.duration = duration;
	}
	public String getInputPath() {
		return inputPath;
	}
	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}
	public int getLoop() {
		return loop;
	}
	public void setLoop(int loop) {
		this.loop = loop;
	}
	public double getStartTime() {
		return startTime;
	}
	public void setStartTime(double startTime) {
		this.startTime = startTime;
	}
	public int writeData(File dir,DougaPreference preference,int startIndex,int maxframe) throws IOException {
		int wf;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		if(getLoop()==0){
			wf=VideoUtils.copyVideo(preference.getFfmpegPath(), getInputPath(), getStartTime(), getDuration(), dir, startIndex, preference.getFps(), preference.getWidth(), preference.getHeight(), maxframe);
			
		}else{
			wf=VideoUtils.loopVideo(preference.getFfmpegPath(), getInputPath(), getStartTime(), getDuration(), getLoop(),getLoopPadTime(),getPadColor(),isCanStopInLoop(),dir, startIndex, preference.getFps(), preference.getWidth(), preference.getHeight(), maxframe);
			
			}
		//sync audio
		if(audioData!=null){
			if(wf>0){
				audioData.setSynch(true);//
				double sl=(double)wf/preference.getFps();
				DougaSystem.log.info("synchlength="+sl+" wf="+wf+" fps="+preference.getFps());
				audioData.setSynchLength(sl);
			}else{
				DougaSystem.log.info("do nothing video");
				
				}
		}
		
		
		
		
		return wf;
	}
	public AudioData getAudioData() {
		return audioData;
	}
	public void setAudioData(AudioData audioData) {
		this.audioData = audioData;
	}
	public double getLoopPadTime() {
		return loopPadTime;
	}
	public void setLoopPadTime(double loopPadTime) {
		this.loopPadTime = loopPadTime;
	}
	public int getPadColor() {
		return padColor;
	}
	public void setPadColor(int padColor) {
		this.padColor = padColor;
	}

	public double getTotalTime() {
		if(loop==DougaSystem.FOREEVER){
			return DougaSystem.FOREEVER;
		}
		int lo=1;
		if(loop>1){
			lo=loop;
		}
		double d=duration;
		if(d==0){
			d=logicalTime;
			d-=startTime;//fix length
		}
		
		
		
		//BUG if it's synch audio wrong.
		return d*lo+(lo-1)*loopPadTime;
	}
	
	public double getLogicalTime() {
		return logicalTime;
	}
	public void setLogicalTime(double logicalTime) {
		this.logicalTime = logicalTime;
	}
	public int writeData(VideoWriter writer, DougaPreference preference) throws IOException {
int wf;

		int startIndex=writer.getWrited()+1;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		if(getLoop()==0){
			wf=VideoUtils.copyVideo(this,writer,preference.getFfmpegPath(), getInputPath(), getStartTime(), getDuration(),preference.getFps(), preference.getWidth(), preference.getHeight());
			
		}else{
			wf=VideoUtils.loopVideo(this,writer,preference.getFfmpegPath(), getInputPath(), getStartTime(), getDuration(), getLoop(),getLoopPadTime(),getPadColor(),isCanStopInLoop(),preference.getFps(), preference.getWidth(), preference.getHeight());
			
			}
		//sync audio
		if(audioData!=null){
			if(wf>0){
				audioData.setSynch(true);//
				double sl=(double)wf/preference.getFps();
				DougaSystem.log.info(sl+",wf="+wf+"fps="+preference.getFps());
				audioData.setSynchLength(sl);
			}else{
				DougaSystem.log.info("do nothing video");
				
				}
		}
		
		
		//TODO if video is too short ,bad things will happen.
		if(wf>0){
			if(endEffect!=null){
				endEffect.setKeyFrame(startIndex+wf-1);
			}
		}else{//0 writed
			if(startEffect!=null){
				startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
			}
		}
		
		return wf;
	}

	
	
}
